package com.authine.cloudpivot.web.api.secondopen.controller;

import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.engine.api.facade.BizObjectFacade;
import com.authine.cloudpivot.engine.api.model.runtime.BizObjectCreatedModel;
import com.authine.cloudpivot.engine.api.model.runtime.WorkflowInstanceModel;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.enums.ProcessType;
import com.authine.cloudpivot.web.api.secondopen.domain.AjaxResult;
import com.authine.cloudpivot.web.api.secondopen.domain.BudgetLog;
import com.authine.cloudpivot.web.api.secondopen.domain.Monthlybudgetadjustrecord;
import com.authine.cloudpivot.web.api.secondopen.modules.CmCrTermsParamDTO;
import com.authine.cloudpivot.web.api.secondopen.modules.CmCrTermsParamVO;
import com.authine.cloudpivot.web.api.secondopen.modules.QadResultVO;
import com.authine.cloudpivot.web.api.secondopen.modules.ThsqParamsDTO;
import com.authine.cloudpivot.web.api.secondopen.service.SalesManagementService;
import com.authine.cloudpivot.web.api.utils.DateUtil;
import com.authine.cloudpivot.web.api.utils.StringUtils;
import io.jsonwebtoken.lang.Collections;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author dw4443
 * @version V1.0
 * @description: 销售管理控制器
 * @date 2021/10/21 9:42
 */
@Api(value = "二开接口", tags = "销售管理控制器")
@RestController
    @RequestMapping("/api/salesmanagement")
public class SalesManagementController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(SalesManagementController.class);

    @Autowired
    private SalesManagementService salesManagementService;

    /**
     * 客户支付方式修改后回传QAD
     */
    @RequestMapping("/returnCmCrTerms")
    public AjaxResult returnCmCrTerms(@RequestBody CmCrTermsParamDTO cmCrTermsParamDTO) {
        log.info("获取参数: cmCrTermsParamDTO :" + JSON.toJSONString(cmCrTermsParamDTO));

        //参数转换
        CmCrTermsParamVO vos = new CmCrTermsParamVO();
        vos.setMdmCmAddr(cmCrTermsParamDTO.getKhbm());
        vos.setMdmCmCrTerms(cmCrTermsParamDTO.getFkzqfkfs());

        //调用service
        QadResultVO qadResultVO = salesManagementService.sendCmCrTerms(vos);
        String code = qadResultVO.getCode();
        String msg = qadResultVO.getMsg();
        Object data = qadResultVO.getData();
        if (code.equals("10000")) {
            return AjaxResult.success(msg, data);
        } else {
            return AjaxResult.error(msg, data);
        }
    }


    /**
     * 【退货申请单】 回传QAD
     *
     * @param objId
     * @return
     */
    @RequestMapping(value = "/returnApplication", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "退货申请表走MO移库单、销售订单两个接口", notes = "退货申请表走MO移库单、销售订单两个接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "objId", value = "业务对象id", dataType = "String", paramType = "query")
    })
    public AjaxResult returnApplication(String objId, String processCode) {
        log.info("获取参数:objId={},processCode={},identtify={}", objId, processCode);
        if (StringUtils.isBlank(objId) || StringUtils.isBlank(processCode)) {
            log.info("参数不完整，直接结束");
            return AjaxResult.error();
        }
        BizObjectFacade bizObjectFacade = super.getBizObjectFacade();
        //获取业务数据
        BizObjectCreatedModel bizObject = bizObjectFacade.getBizObject(processCode, objId);
        if (bizObject == null) {
            log.info("业务对象不存在");
            return AjaxResult.error();
        }
        //流程实例ID
        String wId = bizObject.getWorkflowInstanceId();
        log.info("TargetOwner:wId={}", wId);
        WorkflowInstanceModel wiModel = super.getWorkflowInstanceFacade().getWorkflowInstance(wId);
        if (wiModel == null) {
            log.info("业务流程对象不存在");
        }
        //客户代码
        String khdm = (String) bizObject.get("khdm");
        //截止日期
        Date endtime = (Date) bizObject.get("endtime");
        //退货描述
        String thms = (String) bizObject.get("thms");
        String thly = (String) bizObject.get("thly");
        String loc = (String) bizObject.get("loc");
        String thzwclxx = (String) bizObject.get("thzwclxx");
        String qd = (String) bizObject.get("qd");
        String thzdd = (String) bizObject.get("thzdd");
        String site = (String) bizObject.get("site");
        String djrq = (String) bizObject.get("djrq");

        ThsqParamsDTO thsqParamsDTO = new ThsqParamsDTO();
        thsqParamsDTO.setKhdm(khdm);
        thsqParamsDTO.setEndtime(endtime);
        thsqParamsDTO.setThms(thms);
        thsqParamsDTO.setThly(thly);
        thsqParamsDTO.setLoc(loc);
        thsqParamsDTO.setThzwclxx(thzwclxx);
        thsqParamsDTO.setQd(qd);
        thsqParamsDTO.setThzdd(thzdd);
        thsqParamsDTO.setSite(site);
        thsqParamsDTO.setDjrq(djrq);

        List<Map<String, Object>> thqdList = new ArrayList<>();
        if ("thsqb".equals(processCode)) {
            //如果是退货申请表，获取子表(退货清单)
            thqdList = (List<Map<String, Object>>) bizObject.get("thqd");
            thsqParamsDTO.setThqdList(thqdList);

        }
        QadResultVO qadResultVO = salesManagementService.returnApplication(thsqParamsDTO);
        String code = qadResultVO.getCode();
        String msg = qadResultVO.getMsg();
        Object data = qadResultVO.getData();
        if (code.equals("10000")) {
            return AjaxResult.success(msg, data);
        } else {
            return AjaxResult.error(msg, data);
        }
    }
}